/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.actions.delete;

import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;

import com.cea.papyrus.core.editpart.DiagramEditPart;
import com.cea.papyrus.core.utils.Debug;

/**
 * 
 */
public class DeleteFromDiagramAction extends SelectionAction {

    /**
     * 
     */
    public final static String DELETE_FROM_DIAGRAM_ACTION_REQUEST =
            "com.cea.papyrus.requests.deletefromdiagram";

    /**
     * 
     */
    public final static String DELETE_FROM_DIAGRAM_ACTION_ID =
            "com.cea.papyrus.core.editor.actions.deletefromdiagram";

    /**
     * Request created by this action.
     */
    protected Request request;

    /**
     * Default constructor.
     * 
     * @param part
     */
    public DeleteFromDiagramAction(IWorkbenchPart part) {
        super(part);
        request = new Request(DELETE_FROM_DIAGRAM_ACTION_REQUEST);
        setText("Delete From Diagram");
        setId(DELETE_FROM_DIAGRAM_ACTION_ID);
        setToolTipText("Delete element(s) from the diagram");
        ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
        setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));
        setHoverImageDescriptor(getImageDescriptor());

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.ui.actions.WorkbenchPartAction#calculateEnabled()
     */
    @Override
    protected boolean calculateEnabled() {
        return canPerformAction();
    }

    /**
     * @return
     */
    private boolean canPerformAction() {
        if (getSelectedObjects().isEmpty()) {
            return false;
        }
        List parts = getSelectedObjects();
        for (int i = 0; i < parts.size(); i++) {
            Object o = parts.get(i);

            // boolean t=(!(o instanceof GraphNodeEditPart));
            // boolean v=(!(o instanceof GraphEdgeEditPart));
            // if ((o instanceof DiagramEditPart)||((!(o instanceof GraphNodeEditPart)) &&
            // (!(o instanceof GraphEdgeEditPart)))) {
            if (o instanceof DiagramEditPart)
                return false;
            //
            // @TODO Reenable code
//            // deactivate the action for SeparatorEditPart(InteractionOperand)
//            if (o instanceof SeparatorEditPart) {
//                return false;
//            }
//            if (!(o instanceof GraphNodeEditPart || o instanceof GraphNodeEditPart)) {
//                if (o instanceof com.cea.papyrus.diagram.sequence.sedi.edit.DiagramEditPart)
//                    return false;
//                if (!(o instanceof com.cea.papyrus.diagram.sequence.sedi.edit.AbstractGraphicalEditPart || o instanceof com.cea.papyrus.diagram.sequence.sedi.edit.AbstractLinkEditPart))
//                    return false;
//            }
//            // --- SeDi support End
        }
        return true;
    }

    /**
     * @return
     */
    private Command getCommand() {
        List editparts = getSelectedObjects();
        CompoundCommand cc = new CompoundCommand();
        Debug.debug(this, "Action::getCommand()", 21);
        for (int i = 0; i < editparts.size(); i++) {
            EditPart part = (EditPart) editparts.get(i);
            cc.add(part.getCommand(request));
        }
        return cc;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.action.Action#run()
     */
    @Override
    public void run() {
        // open dialog window
        BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {
            public void run() {
                execute(getCommand());
            }
        });
    }

}